package com.motorola.demomode.facedetect;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;


public class FaceDetectActivity extends Activity {
    private static final String TAG = "FaceDetectActivity";
    int mNumberOfCameras;
    boolean mToEnterBackground;

    static FaceDetectActivity mThis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mThis = this;

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        // Find the total number of cameras available
        mNumberOfCameras = Camera.getNumberOfCameras();

        setContentView(R.layout.hidden_preview);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mThis = null;

        Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_SERVICE_STOP);
        intent.setClass(this, FaceDetectService.class);
        startService(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_SERVICE_START);
        intent.setClass(this, FaceDetectService.class);
        intent.putExtra("isFront", mNumberOfCameras > 1);
        startService(intent);

        // show camera preview
        intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_TO_FOREGROUND);
        intent.setClass(this, FaceDetectService.class);
        startService(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mToEnterBackground) {
            // hide camera preview
            Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_TO_BACKGROUND);
            intent.setClass(this, FaceDetectService.class);
            startService(intent);
        } else {
            Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_SERVICE_STOP);
            intent.setClass(this, FaceDetectService.class);
            startService(intent);
        }
        mToEnterBackground = false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            Log.d(TAG, "to switch camera ...");
            // check for availability of multiple cameras
            if (mNumberOfCameras == 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(this.getString(R.string.camera_alert))
                        .setNeutralButton("Close", null);
                AlertDialog alert = builder.create();
                alert.show();
                return true;
            }

            Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_SWITCH_CAM);
            intent.setClass(this, FaceDetectService.class);
            startService(intent);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_BACK) {
            mToEnterBackground = true;

            // launch home
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory(Intent.CATEGORY_HOME);
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);

            Intent intent = new Intent(FaceDetectService.ACTION_FACE_DETECT_TO_BACKGROUND);
            intent.setClass(this, FaceDetectService.class);
            startService(intent);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

// ----------------------------------------------------------------------
